// 访问接口路由配置

package router

import (
	"admin-go-api/api/controller"
	"admin-go-api/common/config"
	"admin-go-api/middleware"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
)

// InitRouter 初始化路由
func InitRouter() *gin.Engine {
	router := gin.New()
	// 跌机恢复
	router.Use(gin.Recovery())
	//跨域中间件
	router.Use(middleware.Cors())
	// 日志中间件
	router.Use(middleware.Logger())
	// 将本地目录UploadDir 映射到URL路径"/UploadDir" 第一个参数为路径， 第二个参数为本地目录
	router.StaticFS(config.Config.ImageSettings.UploadDir, http.Dir(config.Config.ImageSettings.UploadDir))

	// 路由注册
	register(router)
	return router
}

// 路由注册
func register(router *gin.Engine) {
	router.GET("/api/captcha", controller.Captcha)
	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 登录路由
	router.POST("/api/login", controller.Login)

	// 岗位相关路由
	//router.POST("/api/post/add", controller.CreateSysPost)
	//router.GET("/api/post/info", controller.GetSysPostById)
	//router.PUT("/api/post/update", controller.UpdateSysPost)
	//router.DELETE("/api/post/delete", controller.DeleteSysPostById)
	//router.DELETE("/api/post/batch/delete", controller.BatchDeleteSysPost)
	//router.PUT("/api/post/updateStatus", controller.UpdateSysPostStatus)
	//router.GET("/api/post/list", controller.GetSysPostList)
	//router.GET("/api/post/vo/list", controller.QuerySysPostVoList)

	// jwt鉴权接口、操作日志接口  每次调用接口，都需要鉴权以及记录操作日志
	jwt := router.Group("/api", middleware.AuthMiddleware(), middleware.LogMiddleware())
	{
		// 岗位相关
		post := jwt.Group("/post")
		{
			post.POST("/add", controller.CreateSysPost)
			post.GET("/info", controller.GetSysPostById)
			post.PUT("/update", controller.UpdateSysPost)
			post.DELETE("/delete", controller.DeleteSysPostById)
			post.DELETE("/batch/delete", controller.BatchDeleteSysPost)
			post.PUT("/updateStatus", controller.UpdateSysPostStatus)
			post.GET("/list", controller.GetSysPostList)
			post.GET("/vo/list", controller.QuerySysPostVoList)
		}

		// 部门相关路由
		dept := jwt.Group("/dept")
		{
			dept.POST("/add", controller.CreateSysDept)
			dept.GET("/vo/list", controller.QuerySysDeptVoList)
			dept.GET("/info", controller.GetSysDeptById)
			dept.PUT("/update", controller.UpdateSysDept)
			dept.GET("/list", controller.GetSysDeptList)
			dept.DELETE("/delete", controller.DeleteSysDeptById)
		}

		// 菜单相关路由
		menu := jwt.Group("/menu")
		{
			menu.GET("/list", controller.GetSysMenuList)
			menu.DELETE("/delete", controller.DeleteSysMenu)
			menu.POST("/update", controller.UpdateSysMenu)
			menu.GET("/info", controller.GetSysMenu)
			menu.POST("/add", controller.CreateSysMenu)
			menu.GET("/vo/list", controller.QuerySysMenuVoList)
		}

		// 角色相关路由
		role := jwt.Group("/role")
		{
			role.PUT("/vo/assignPermissions", controller.AssignPermissions)
			role.GET("/vo/idList", controller.QueryRoleMenuIdList)
			role.GET("/vo/list", controller.QuerySysRoleVoList)
			role.GET("/list", controller.GetSysRoleList)
			role.PUT("/updateStatus", controller.UpdateSysRoleStatus)
			role.DELETE("/delete", controller.DeleteSysRoleById)
			role.PUT("/update", controller.UpdateSysRole)
			role.GET("/info", controller.GetSysRoleById)
			role.POST("/add", controller.CreateSysRole)
		}

		// 用户相关路由
		admin := jwt.Group("/admin")
		{
			admin.POST("/add", controller.CreateSysAdmin)
			admin.GET("/info", controller.GetSysAdminInfo)
			admin.PUT("/update", controller.UpdateSysAdmin)
			admin.DELETE("/delete", controller.DeleteSysAdminById)
			admin.PUT("/updateStatus", controller.UpdateSysAdminStatus)
			admin.PUT("/updatePassword", controller.ResetSysAdminPassword)
			admin.GET("/list", controller.GetSysAdminList)
			admin.PUT("/updatePersonal", controller.UpdatePersonal)
			admin.PUT("/updatePersonalPassword", controller.UpdatePersonalPassword)
		}

		// 上传文件路由
		jwt.POST("/upload", controller.UpLoad)

		// 登录日志相关
		loginInfo := jwt.Group("/sysLoginInfo")
		{
			loginInfo.GET("/list", controller.GetSysLoginInfoList)
			loginInfo.DELETE("/batch/delete", controller.BatchDeleteSysLoginInfo)
			loginInfo.DELETE("/delete", controller.DeleteSysLoginInfoById)
			loginInfo.DELETE("/clean", controller.CleanSysLoginInfo)
		}

		// 操作日志相关
		operationLog := jwt.Group("/sysOperationLog")
		{
			operationLog.GET("/list", controller.GetSysOperationLogList)
			operationLog.DELETE("/batch/delete", controller.BatchDeleteSysOperationLog)
			operationLog.DELETE("/delete", controller.DeleteOperationLogById)
			operationLog.DELETE("/clean", controller.CleanSysOperationLog)
		}
	}
}
